package com.ruoyi.wxarea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.wxarea.service.IAddressUserService;
import com.ruoyi.wxarea.mapper.AddressUserMapper;
import com.ruoyi.wxarea.pojo.AddressUser;
import com.ruoyi.wxcommon.common.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2024-05-06
 */
@Service
public class AddressUserServiceImpl extends ServiceImpl<AddressUserMapper, AddressUser> implements IAddressUserService {


    @Override
    public Result selectUserAddressById(Integer pageIndex, Integer pageSize, Integer wxId) {
        Page<AddressUser> page = new Page<>(pageIndex,pageSize);
        QueryWrapper<AddressUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_id",wxId).eq("is_delete",1);
        Page<AddressUser> page1 = this.page(page, queryWrapper);
        return Result.SUCCESS(page1);
    }
    @Transactional
    @Override
    public Result addAddress(String url, String address, String detail, Integer wxId, Integer id) {

        if ("add".equals(url)){
            AddressUser addressUser = AddressUser.builder().address(address).detail(detail).wxId(wxId).build();
            this.baseMapper.insert(addressUser);
            return Result.SUCCESS(addressUser.getId());
        }else if ("update".equals(url)){
            AddressUser addressUser = AddressUser.builder().address(address).detail(detail).id(id).build();
            this.baseMapper.updateById(addressUser);
        }else {
            this.baseMapper.deleteById(id);
        }
        return Result.SUCCESS();
    }
}
